package com.xjrccb.meeting.servlet;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xjrccb.meeting.bean.Meeting;
import com.xjrccb.meeting.dao.AddMeetingDao;
@Controller
@RequestMapping("/form/*")
public class AddMeeting {
	@Autowired
	private AddMeetingDao add ;
	
	
	@RequestMapping(value="addMeeting.do",produces="text/html;charset=UTF-8")
    public @ResponseBody String addMeeting(HttpServletRequest request,HttpServletResponse response) throws Exception{
	 	Meeting meeting = new Meeting();
 		Field[] fields = meeting.getClass().getDeclaredFields();
 		for (int i = 0; i < fields.length; i++) {
 		    // 获取属性的名字
 		    String names = fields[i].getName();
 		    // 将属性的首字符大写，方便构造get，set方法
 		    String name = names.substring(0, 1).toUpperCase() + names.substring(1);
 		    // 获取属性的类型
 		    String type = fields[i].getGenericType().toString();
 		    // 如果type是类类型，则前面包含"class "，后面跟类名
            //set值
            Class[] parameterTypes = new Class[1];
            parameterTypes[0] = fields[i].getType();
            Method m = meeting.getClass().getMethod("set" + name, parameterTypes);
            System.out.println(request.getParameter(names));
            m.invoke(meeting, request.getParameter(names));
 		}
	 	boolean b= add.insertMeeting(meeting);
        return b?"suc":"fail";
    }

	@RequestMapping(value="queryMeeting.do",produces="text/html;charset=UTF-8")
    public @ResponseBody String queryMeeting(HttpServletRequest request,HttpServletResponse response) throws Exception{
	 	List<Meeting> list = add.queryMeeting(request.getParameter("start"), request.getParameter("end"));
	 	JSONArray resultArray = new JSONArray();
	 	for(Meeting me :list){
	 		JSONObject obj1 = new JSONObject(); 
	 		obj1.put("title", me.getMeetingname());
	 		obj1.put("start", me.getStartTime());
	 		obj1.put("end", me.getEndTime());
	 		obj1.put("id", me.getId());
	 		obj1.put("place", me.getPlace());
	 		resultArray.put(obj1);
	 	}
        return resultArray.toString();
    }
	
	@RequestMapping(value="queryDetail.do",produces="text/html;charset=UTF-8")
    public @ResponseBody String queryDetail(HttpServletRequest request,HttpServletResponse response) throws Exception{
	 	Meeting meet = add.queryDetail(request.getParameter("id"));
	 	StringBuffer sb = new StringBuffer();
	 	sb.append("<div id=\"modal-title\" class=\"modal-header\">").append(" <h3 id=\"md-event-title\">").append(meet.getMeetingname())
	 		.append("</h3></div><div class=\"modal-body\"> <form class=\"form-horizontal\" id=\"show-form\">")
	 		.append("<div class=\"form-group\"> <label class=\"control-label col-sm-6\">").append(meet.getMeetingname())
	 		.append("</label></div> <div class=\"form-group\">  <label id=\"labelMeetingDate\" class=\"control-label col-sm-2\">开始时间</label>")
	 		.append("<div class=\"col-sm-10\"  >").append(meet.getStartTime())
	 		.append("</div></div><div class=\"form-group\"><label id=\"labelMeetingDate\" class=\"control-label col-sm-2\">结束时间</label>  <div class=\"col-sm-10\"  >")
	 		.append(meet.getEndTime()).append("</div> </div> <div class=\"form-group \"> <label for=\"meetingForm_owner\" class=\"control-label col-sm-2\">预订人</label>")
	 		.append("<div class=\"col-sm-10\"  >")
	 		.append("")
	 		.append("</div> </div> <div class=\"form-group \"> ")
	 		.append("<label for=\"meetingForm_meetingMembers\" class=\"control-label col-sm-2\">参与人员</label> <div class=\"col-sm-10\" >")
	 		.append(meet.getPersons())
	 		.append("</div></div> </form> </div> <div class=\"modal-footer\"> <button type=\"button\" class=\"btn btn-default\" data-dismiss=\"modal\">关闭</button> ")
	 		.append("<button type=\"button\" class=\"btn btn-primary\" onclick=\"deleteMeeting();\">取消会议</button></div>");
        return sb.toString();
    }
	
	@RequestMapping(value="delMeeting.do",produces="text/html;charset=UTF-8")
    public @ResponseBody String delMeeting(HttpServletRequest request,HttpServletResponse response) throws Exception{
	 	boolean b = add.deleteMeeting(request.getParameter("id"));
	 	return b?"suc":"fail";
    }
	
	public void setAdd(AddMeetingDao add) {
		this.add = add;
	}
	 
	 
}
